﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 代码维护
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class ChkInfoStdController : Common.AdminBaseController
    {
        //
        // GET: /Admin/Code/
        /// <summary>
        /// 代码维护主页
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_ChkInfoStd", Order = 1)]
        [LogPageActionFilter("考核基分定义", Order = 2)]
        public ActionResult Index(string year)
        {
            if (year == null || year == "")
            {
                year = DateTime.Now.Year.ToString();
            }
            BLL.ChkInfoStdLocal ChkInfoStdLocalBLL = new BLL.ChkInfoStdLocal();
            DataSet ds = ChkInfoStdLocalBLL.GetYeat(Common.GlobalConstant.ENTITYNO);
            if (ds.Tables[0].Rows.Count == 0)
            {
                ViewData["minyear"] = year;
                ViewData["maxyear"] = year;
            }
            else
            {
                ViewData["maxyear"] = ds.Tables[0].AsEnumerable().Select(t => t.Field<decimal>("FYear")).Max();
                ViewData["minyear"] = ds.Tables[0].AsEnumerable().Select(t => t.Field<decimal>("FYear")).Min();
            }
            
            ViewData["Table"] = GetTable(year);
            ViewData["dqyear"] = year;
            return View();
        }


        public ActionResult Save()
        {
            string year = Fetch.Post("hdyear");
            BLL.HRDept HRDeptBLL = new BLL.HRDept();
            BLL.ChkInfoCls chkInfoClsBLL = new BLL.ChkInfoCls();
            try
            {
                List<Model.HRDept> hRDeptList = HRDeptBLL.GetListByEntityNo(Common.GlobalConstant.ENTITYNO);
                List<Model.ChkInfoCls> chkInfoClsList = chkInfoClsBLL.GetListByYear(Common.GlobalConstant.ENTITYNO, decimal.Parse(Fetch.Post("hdyear")));
                List<Model.ChkInfoStdLocal> chkInfoStdLocalList = new List<Model.ChkInfoStdLocal>();
                List<Model.ChkInfoStdSuper> ChkInfoStdSuperList = new List<Model.ChkInfoStdSuper>();
                List<Model.ChkInfoCls> selectchkInfoClsList = (from nm in chkInfoClsList where nm.FPreInfoClsNo == null || nm.FPreInfoClsNo == "" select nm).ToList();
                foreach (Model.HRDept deptModel in hRDeptList)
                {
                    Model.ChkInfoStdLocal chkInfoStdLocalModel = new Model.ChkInfoStdLocal();
                    chkInfoStdLocalModel.FBaseScore = decimal.Parse((Fetch.Post("FBaseScoreLocal" + deptModel.FDeptNo) == "" ? "0" : Fetch.Post("FBaseScoreLocal" + deptModel.FDeptNo)));
                    chkInfoStdLocalModel.FDeptNo = deptModel.FDeptNo;
                    chkInfoStdLocalModel.FEntityNo = Common.GlobalConstant.ENTITYNO;
                    chkInfoStdLocalModel.FYear = decimal.Parse(Fetch.Post("hdyear"));
                    chkInfoStdLocalList.Add(chkInfoStdLocalModel);

                    foreach (Model.ChkInfoCls model in selectchkInfoClsList)
                    {
                        Model.ChkInfoStdSuper ChkInfoStdSuperModel = new Model.ChkInfoStdSuper();
                        ChkInfoStdSuperModel.FBaseScore = decimal.Parse((Fetch.Post("FBaseScoreSuper" + deptModel.FDeptNo + model.FInfoClsNo) == "" ? "0" : Fetch.Post("FBaseScoreSuper" + deptModel.FDeptNo + model.FInfoClsNo)));
                        ChkInfoStdSuperModel.FDeptNo = deptModel.FDeptNo;
                        ChkInfoStdSuperModel.FEntityNo = Common.GlobalConstant.ENTITYNO;
                        ChkInfoStdSuperModel.FInfoClsNo = model.FInfoClsNo;
                        ChkInfoStdSuperModel.FYear = decimal.Parse(Fetch.Post("hdyear"));
                        ChkInfoStdSuperList.Add(ChkInfoStdSuperModel);
                    }
                }

                foreach (Model.ChkInfoCls model in selectchkInfoClsList)
                {
                    Model.ChkInfoStdSuper ChkInfoStdSuperModel = new Model.ChkInfoStdSuper();
                    ChkInfoStdSuperModel.FBaseScore = decimal.Parse((Fetch.Post("FBaseScoreSuperAllSum" + model.FInfoClsNo) == "" ? "0" : Fetch.Post("FBaseScoreSuperAllSum" + model.FInfoClsNo)));
                    ChkInfoStdSuperModel.FDeptNo = "ALL";
                    ChkInfoStdSuperModel.FInfoClsNo = model.FInfoClsNo;
                    ChkInfoStdSuperModel.FEntityNo = Common.GlobalConstant.ENTITYNO;
                    ChkInfoStdSuperModel.FYear = decimal.Parse(Fetch.Post("hdyear"));
                    ChkInfoStdSuperList.Add(ChkInfoStdSuperModel);
                }


                BLL.ChkInfoStdLocal ChkInfoStdLocalBLL = new BLL.ChkInfoStdLocal();
                ChkInfoStdLocalBLL.addChkInfoLoaclAndSuper(chkInfoStdLocalList, ChkInfoStdSuperList, Common.GlobalConstant.ENTITYNO, decimal.Parse(year));

                ViewData["jsAlert"] = "操作成功";
            }
            catch
            {
                ViewData["jsAlert"] = "系统异常";
            }
            if (year == null || year == "")
            {
                year = DateTime.Now.Year.ToString();
            }

            ViewData["Table"] = GetTable(year);
            ViewData["dqyear"] = year;
            ViewData["maxyear"] = year;
            ViewData["minyear"] = year;
            return View("Index");
        }

        public string GetTable(string year)
        {
            BLL.HRDept HRDeptBLL = new BLL.HRDept();
            BLL.ChkInfoCls chkInfoClsBLL = new BLL.ChkInfoCls();
            BLL.ChkInfoStdLocal chkInfoStdLocalBLL = new BLL.ChkInfoStdLocal();
            BLL.ChkInfoStdSuper chkInfoStdSuperBLL = new BLL.ChkInfoStdSuper();

            List<Model.HRDept> hRDeptList = HRDeptBLL.GetListByEntityNo(Common.GlobalConstant.ENTITYNO);
            List<Model.ChkInfoCls> chkInfoClsListOld = chkInfoClsBLL.GetListByYear(Common.GlobalConstant.ENTITYNO, decimal.Parse(year));
            List<Model.ChkInfoCls> chkInfoClsList = (from nm in chkInfoClsListOld where nm.FisExamine == "Y" select nm).ToList();
            List<Model.ChkInfoStdLocal> chkInfoStdLocalList = chkInfoStdLocalBLL.GetModelList(" FYear ='" + year + "' and FEntityNo='" + Common.GlobalConstant.ENTITYNO + "'");
            List<Model.ChkInfoStdSuper> chkInfoStdSuperList = chkInfoStdSuperBLL.GetModelList(" FYear ='" + year + "' and FEntityNo='" + Common.GlobalConstant.ENTITYNO + "'");

            System.Text.StringBuilder sbr = new System.Text.StringBuilder();
            sbr.Append("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"tableForm2\">");

            //绑定标题行

            sbr.Append("<tr height=\"25px\">");
            sbr.Append("<th style =' text-align:center;  width:150px'>部门</th>");

            sbr.Append("<th style =' text-align:center'>本级</th>");
            List<Model.ChkInfoCls> selectchkInfoClsList=(from nm in chkInfoClsList where nm.FPreInfoClsNo == null || nm.FPreInfoClsNo=="" select nm ).ToList();

            foreach (Model.ChkInfoCls model in selectchkInfoClsList)
            {
                sbr.Append("<th style =' text-align:center'>" + model.FInfoClsName + "</th>");
            }
            sbr.Append("<th style =' text-align:center;width:100px'>合计</th>");
            sbr.Append("</tr>");

            foreach (Model.HRDept deptModel in hRDeptList)
            {
                decimal sum = 0;
                List<Model.ChkInfoStdLocal> selectChkInfoStdLocal = (from nm in chkInfoStdLocalList where nm.FDeptNo == deptModel.FDeptNo select nm).ToList();
                sbr.Append("<tr height=\"25px\">");
                sbr.Append("<td align=\"center\"  class='tdClass'>" + deptModel.FDeptName + "</td>");

                if (selectChkInfoStdLocal.Count == 0)
                {
                    sbr.Append("<td align=\"center\"  class='tdClass'><input type='text' id='FBaseScoreLocal' style='text-align:right' name='FBaseScoreLocal" + deptModel.FDeptNo + "'  onkeyup=\"clearNoNum(this)\" onafterpaste=\"clearNoNum(this)\" onfocus=\"tbFocus(this)\" onblur=\"ValueSum('deptSum" + deptModel.FDeptNo + "','LoanAllSum','LoanSum',this,'AllSum')\" value='0' style='width:100px'></td>");
                }
                else
                {
                    sbr.Append("<td align=\"center\"  class='tdClass'><input type='text' id='FBaseScoreLocal'  style='text-align:right' name='FBaseScoreLocal" + deptModel.FDeptNo + "'  onkeyup=\"clearNoNum(this)\" onafterpaste=\"clearNoNum(this)\" onfocus=\"tbFocus(this)\" onblur=\"ValueSum('deptSum" + deptModel.FDeptNo + "','LoanAllSum','LoanSum',this,'AllSum')\" value='" + selectChkInfoStdLocal[0].FBaseScore + "' style='width:100px'></td>");
                    sum += (selectChkInfoStdLocal[0].FBaseScore == null ? 0 : decimal.Parse(selectChkInfoStdLocal[0].FBaseScore.ToString()));
                }

                foreach (Model.ChkInfoCls model in selectchkInfoClsList)
                {
                    List<Model.ChkInfoStdSuper> selectchkInfoStdSuperList = (from nm in chkInfoStdSuperList where nm.FDeptNo == deptModel.FDeptNo && nm.FInfoClsNo == model.FInfoClsNo select nm).ToList();

                    if (selectchkInfoStdSuperList.Count == 0)
                    {
                        sbr.Append("<td align=\"center\"  class='tdClass'><input type='text' id='FBaseScoreSuper'  style='text-align:right' name='FBaseScoreSuper" + deptModel.FDeptNo + model.FInfoClsNo + "'  onkeyup=\"clearNoNum(this)\" onafterpaste=\"clearNoNum(this)\" onfocus=\"tbFocus(this)\" onblur=\"ValueSum('deptSum" + deptModel.FDeptNo + "','SuperSum" + model.FInfoClsNo + "','',this,'AllSum')\" value='0' style='width:100px'></td>");
                    }
                    else
                    {
                        sbr.Append("<td align=\"center\"  class='tdClass'><input type='text' id='FBaseScoreSuper'  style='text-align:right' name='FBaseScoreSuper" + deptModel.FDeptNo + model.FInfoClsNo + "'  onkeyup=\"clearNoNum(this)\" onafterpaste=\"clearNoNum(this)\" onfocus=\"tbFocus(this)\" onblur=\"ValueSum('deptSum" + deptModel.FDeptNo + "','SuperSum" + model.FInfoClsNo + "','',this,'AllSum')\" value='" + selectchkInfoStdSuperList[0].FBaseScore + "' style='width:100px'></td>");
                        sum += (selectchkInfoStdSuperList[0].FBaseScore == null ? 0 : decimal.Parse(selectchkInfoStdSuperList[0].FBaseScore.ToString()));
                    }
                }

                sbr.Append("<td align=\"center\"  class='tdClass' style='text-align:right'>");
                sbr.Append("<span id=\"");
                sbr.Append("deptSum" + deptModel.FDeptNo);
                sbr.Append("\" >");
                sbr.Append(sum);
                sbr.Append("</span>");

                //sbr.Append("<input type='text' id='deptSum" + deptModel.FDeptNo + "' disabled  style='text-align:right' name='deptSum" + deptModel.FDeptNo + "'  onkeyup=\"clearNoNum(this)\" onafterpaste=\"clearNoNum(this)\" onfocus=\"tbFocus(this)\" onblur=\"ValueSum('deptSum" + deptModel.FDeptNo + "','LoanAllSum','LoanSum',this,'AllSum')\" value='" + selectChkInfoStdLocal[0].FBaseScore + "' style='width:100px'>");
                sbr.Append("</tr>");
            }



            decimal? deptSum = chkInfoStdLocalList.Sum(t => t.FBaseScore);

            sbr.Append("<tr height=\"25px\">");
            sbr.Append("<td align=\"center\"  class='tdClass'>合计</td>");
            sbr.Append("<td align=\"center\"  class='tdClass' style='text-align:right'>");
            sbr.Append("<span id=\"");
            sbr.Append("LoanSum");
            sbr.Append("\"");
            sbr.Append(deptSum);
            sbr.Append("</span>");
            sbr.Append("</td>");
            decimal allSum = decimal.Parse(deptSum.ToString());
            foreach (Model.ChkInfoCls model in selectchkInfoClsList)
            {
                List<Model.ChkInfoStdSuper> selectchkInfoStdSuperList = (from nm in chkInfoStdSuperList where nm.FInfoClsNo == model.FInfoClsNo && nm.FDeptNo != "ALL" select nm).ToList();
                decimal? ClsSum = selectchkInfoStdSuperList.Sum(t => t.FBaseScore);
                sbr.Append("<td align=\"center\"  class='tdClass' style='text-align:right'>");
                sbr.Append("<span id=\"");
                sbr.Append("SuperSum" + model.FInfoClsNo);
                sbr.Append("\">");
                sbr.Append(ClsSum);
                sbr.Append("</span>");
                sbr.Append("</td>");
                allSum += (ClsSum == null ? 0 : decimal.Parse(ClsSum.ToString()));
                
            }

            sbr.Append("<td align=\"center\"  class='tdClass' style='text-align:right' >");
            sbr.Append("<span id=\"");
            sbr.Append("AllSum");
            sbr.Append("\">");
            sbr.Append(allSum);
            sbr.Append("</span>");
            sbr.Append("</td>");
            sbr.Append("</tr>");


            sbr.Append("<tr height=\"25px\">");
            sbr.Append("<td align=\"center\"  class='tdClass'>全局</td>");
            sbr.Append("<td align=\"center\"  class='tdClass' style='text-align:right' >");
            sbr.Append("<span id=\"");
            sbr.Append("LoanAllSum");
            sbr.Append("\" >");
            sbr.Append(deptSum);
            sbr.Append("</span>");
            sbr.Append("</td>");
            allSum = decimal.Parse(deptSum.ToString());
            foreach (Model.ChkInfoCls model in selectchkInfoClsList)
            {
                List<Model.ChkInfoStdSuper> selectchkInfoStdSuperList = (from nm in chkInfoStdSuperList where nm.FInfoClsNo == model.FInfoClsNo && nm.FDeptNo == "ALL" select nm).ToList();

                if (selectchkInfoStdSuperList.Count == 0)
                {
                    sbr.Append("<td align=\"center\"  class='tdClass'><input type='text' id='FBaseScoreSuperAllSum'  style='text-align:right' name='FBaseScoreSuperAllSum" + model.FInfoClsNo + "'  onkeyup=\"clearNoNum(this)\" onafterpaste=\"clearNoNum(this)\" onfocus=\"tbFocus(this)\" onblur=\"ValueSum('qjAllSum','','',this,'noSum')\" value='0'  style='width:100px'></td>");
                }
                else
                {
                    sbr.Append("<td align=\"center\"  class='tdClass'><input type='text' id='FBaseScoreSuperAllSum'  style='text-align:right' name='FBaseScoreSuperAllSum" + model.FInfoClsNo + "'  onkeyup=\"clearNoNum(this)\" onafterpaste=\"clearNoNum(this)\" onfocus=\"tbFocus(this)\" onblur=\"ValueSum('qjAllSum','','',this,'noSum')\" value='" + selectchkInfoStdSuperList[0].FBaseScore + "' style='width:100px'></td>");
                    allSum += (selectchkInfoStdSuperList[0].FBaseScore == null ? 0 : decimal.Parse(selectchkInfoStdSuperList[0].FBaseScore.ToString()));
                }
            }
            sbr.Append("<td align=\"center\"  class='tdClass' style='text-align:right' >");
            sbr.Append("<span id=\"");
            sbr.Append("qjAllSum");
            sbr.Append("\" >");
            sbr.Append(allSum);
            sbr.Append("</span>");
            sbr.Append("</td>");
            sbr.Append("</tr>");


            sbr.Append("</table>");
            return sbr.ToString();
        }

    }
}
